//
//  HOWelcomeControllerViewController.m
//  WeiDu_1_0
//
//  Created by foguo-mac-1 on 01/07/16.
//  Copyright (c) 2016 WeiDu Energy Tech. All rights reserved.
//

#import "HOWelcomeController.h"
#import "HOUtils.h"
#import "HOUIMgr.h"
#import "HOLoginViewController.h"

@interface HOWelcomeController ()

@end

@implementation HOWelcomeController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
	imgSwipView = [[SwipeView alloc] initWithFrame:CGRectMake(0,
                                                              0,
                                                              self.view.frame.size.width,
                                                              [HOUtils getScreenHeight])];
    imgSwipView.alignment = SwipeViewAlignmentCenter;
    imgSwipView.pagingEnabled = YES;
    imgSwipView.itemsPerPage = 1;
    imgSwipView.truncateFinalPage = YES;
    //configure swipe view
    imgSwipView.delegate = self;
    imgSwipView.dataSource = self;
    [imgSwipView reloadData];
    [self.view addSubview:imgSwipView];
    
    
    pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0,
                                                                 imgSwipView.frame.origin.y + imgSwipView.frame.size.height - 50,
                                                                 self.view.frame.size.width,
                                                                 20)];
    pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
    pageControl.pageIndicatorTintColor = [UIColor grayColor];
    pageControl.currentPage = 0;
    pageControl.numberOfPages = 3;//[self.languageModelArray count];
	pageControl.enabled = YES;
	//[self.view addSubview:pageControl];
	
	view0 = [[UIView alloc] initWithFrame:self.view.frame];
	UIImageView *imageView0 = [[UIImageView alloc]initWithFrame:view0.frame];
	imageView0.image = [UIImage imageNamed:@"yindaoye_1"];
	[view0 addSubview:imageView0];
	[imageView0 release];
	
	view1 = [[UIView alloc] initWithFrame:self.view.frame];
	UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:view1.frame];
	imageView1.image = [UIImage imageNamed:@"yindaoye_2"];
	[view1 addSubview:imageView1];
	[imageView1 release];
	
	view2 = [[UIButton alloc] initWithFrame:self.view.frame];
	UIImageView *imageView2 = [[UIImageView alloc]initWithFrame:view2.frame];
	[view2 setBackgroundImage:[UIImage imageNamed:@"yindaoye_3"] forState:UIControlStateNormal];
	[view2 setBackgroundImage:[UIImage imageNamed:@"yindaoye_3"] forState:UIControlStateSelected];
	[view2 setBackgroundImage:[UIImage imageNamed:@"yindaoye_3"] forState:UIControlStateHighlighted];
	[view2 addSubview:imageView2];
	[view2 addTarget:self action:@selector(gotoLogin:) forControlEvents:UIControlEventTouchUpInside];

	
//	UIButton *loginBtn = [self getButtonWithTitle:NSLocalizedString(@"登  录", @"登  录")];
//	loginBtn.frame = CGRectMake(30,
//								view2.frame.size.height - 100,
//								(view2.frame.size.width - 60),
//								40);
//	[loginBtn addTarget:self action:@selector(gotoLogin:) forControlEvents:UIControlEventTouchUpInside];
//	[view2 addSubview:loginBtn];
	
//	UIButton *registerBtn = [self getButtonWithTitle:NSLocalizedString(@"注  册", @"注  册")];
//	registerBtn.frame = CGRectMake(10 + loginBtn.frame.size.width + loginBtn.frame.origin.x,
//								loginBtn.frame.origin.y,
//								loginBtn.frame.size.width,
//								loginBtn.frame.size.height);
//	[registerBtn addTarget:self action:@selector(gotoRegister:) forControlEvents:UIControlEventTouchUpInside];
//	[view2 addSubview:registerBtn];
	
	
	[imageView2 release];
    
//    [self.view addSubview:pageControl];

}
- (void)gotoLogin:(id)sender
{
	HOLoginViewController *viewController = [[HOLoginViewController alloc] initWithModule:HO_MODULE_TYPE_USER];
	[self.navigationController pushViewController:viewController animated:YES];
	[viewController release];
	
}

- (UIButton *)getButtonWithTitle:(NSString *)title
{
	UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
	btn.frame = CGRectMake(25,
								view2.frame.size.height - 100,
								(view2.frame.size.width - 60) / 2,
								40);
	[btn setTitleColor:MAIN_COLOR_NORMAL forState:UIControlStateNormal];
	[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
	[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
	
	[btn setTitle:title forState:UIControlStateNormal];
	[btn setTitle:title forState:UIControlStateSelected];
	[btn setTitle:title forState:UIControlStateHighlighted];
	
	[btn setBackgroundImage:[HOUtils createImageWithColor:[UIColor whiteColor] forView:btn] forState:UIControlStateNormal];
//	[btn setBackgroundImage:[HOUtils createImageWithColor:MAIN_COLOR_NORMAL forView:btn] forState:UIControlStateSelected];
//	[btn setBackgroundImage:[HOUtils createImageWithColor:MAIN_COLOR_NORMAL forView:btn] forState:UIControlStateHighlighted];
	
	btn.layer.cornerRadius = 3;
	[btn.layer setMasksToBounds:YES];
	
	return btn;
}

- (void)dealloc
{
	[HOUtils removeFromSuperViewForViews:imgSwipView, pageControl, view0, view1, view2];
    [imgSwipView release];
	[pageControl release];
	[view0 release];
	[view1 release];
	[view2 release];
    [super dealloc];
}
- (void)viewWillAppear:(BOOL)animated
{
    
    self.navigationController.navigationBar.hidden = YES;
    [super viewWillAppear:animated];
}

#pragma mark
#pragma mark - swipe SwipeViewDelegate/SwipeViewDataSource implement methods

- (NSInteger)numberOfItemsInSwipeView:(SwipeView *)swipeView
{
    return 3;
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
//    if(imgSwipView == nil || imgSwipView.currentItemView == nil)
//        return bigImgView;
    UIView *frameView = imgSwipView.currentItemView.subviews.firstObject;
    return frameView.subviews.firstObject;
}
- (UIView *)swipeView:(SwipeView *)swipeView viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
	
	if(index == 0)
		return view0;
	else if(index == 1)
		return view1;
	else if(index == 2)
		return view2;
//    if (view == nil)
//    {
//        NSLog(@"reusing view:index: %d", (int)index);
//        UIView *frameView = [[UIView alloc]initWithFrame:CGRectMake(0,
//                                                                    0,
//                                                                    swipeView.frame.size.width,
//                                                                    swipeView.frame.size.height)];
//        
//        
//        
//        UIImage *img = nil;
//        if(index == 0)
//            img = [UIImage imageNamed:@"img_welcome_1"];
//        else if(index == 1)
//            img = [UIImage imageNamed:@"img_welcome_2"];
//        else if(index == 2)
//            img = [UIImage imageNamed:@"img_welcome_3"];
//        UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
//        imgView.contentMode = UIViewContentModeScaleAspectFill;
//        imgView.frame = CGRectMake(0,
//                                   0,
//                                   swipeView.frame.size.width,
//                                   swipeView.frame.size.height);
//        if(IS_IPHONE_4_SCREEN)
//        {
//            imgView.frame = CGRectMake(0,
//                                       - 20 ,
//                                       swipeView.frame.size.width,
//                                       swipeView.frame.size.height);
//        }
//        else
//        {
//            imgView.frame = CGRectMake(0,
//                                       0,
//                                       swipeView.frame.size.width,
//                                       swipeView.frame.size.height);
//        }
//       
//        
//       
//        imgView.tag = 4;
//        [frameView addSubview:imgView];
//        view = frameView;
//    }
//
//    UIImageView *imgView = (UIImageView *)[view viewWithTag:4];
//    UIImage *img = nil;
//    if(index == 0)
//        img = [UIImage imageNamed:@"img_welcome_1"];
//    else if(index == 1)
//        img = [UIImage imageNamed:@"img_welcome_2"];
//    else if(index == 2)
//        img = [UIImage imageNamed:@"img_welcome_3"];
//    imgView.image = img;
//    imgView.contentMode = UIViewContentModeScaleAspectFill;
//    if(IS_IPHONE_4_SCREEN)
//    {
//        imgView.frame = CGRectMake(0,
//                                   - 20 ,
//                                   swipeView.frame.size.width,
//                                   swipeView.frame.size.height);
//    }
//    else
//    {
//        imgView.frame = CGRectMake(0,
//                                   0,
//                                   swipeView.frame.size.width,
//                                   swipeView.frame.size.height);
//    }
	
    return view;
}

- (void)swipeView:(SwipeView *)swipeView didSelectItemAtIndex:(NSInteger)index
{
}

- (void)swipeViewDidEndDragging:(SwipeView *)swipeView willDecelerate:(BOOL)decelerate
{
}
- (void)enterMainScreen
{
    if(imgSwipView.currentItemIndex == 2)
    {
//        NSLog(@"ok");
        UIViewController *viewController = [[HOUIMgr sharedInstance] getFirstViewController];
        [self.navigationController pushViewController:viewController animated:YES];
    }
}


- (void)swipeViewDidEndDecelerating:(SwipeView *)swipeView;
{
//    NSLog(@"swipeViewDidEndDecelerating, index: %d",(int)swipeView.currentItemIndex);
    // [self.viewModel changeLanguageWithIndex:self.swipeView.currentItemIndex];
	pageControl.currentPage = swipeView.currentItemIndex;
//	btn.hidden = swipeView.currentItemIndex != 2;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
